<?php

abstract class Gtux_Model_Estructura extends Gtux_Model_Abstract
{
  /**
   * @var Zend_Date
   */
  protected $_created_at;
  /**
   * @var Gtux_Model_User
   */
  protected $_user;

  /**
   * @param $name
   * @param $value
   */
  public function __set ($name, $value)
  {
    if ($name == 'created_at') 
    {
      if (is_string($value))
      {
        $created_at = new Zend_Date($value, Zend_Date::ISO_8601);
      }
      elseif ($value instanceof Zend_Date) 
      {
        $created_at = $value;
      }
      $this->setCreatedAt($created_at);
    }
    else 
    {
      parent::__set($name, $value);
    }
  }
  
  /**
   * 
   * @return Gtux_Model_Gateway_Abstract 
   * @see Gtux_Model_Abstract::getGateway()
   */
  public function getGateway()
  {
    throw new LogicException('Debe implementar método getGateway');
  }
  
  /**
   * Devuelve el id del usuario que creó el formulario
   *
   * @return int
   */
  public function getIdUser()
  {
    if (!$this->_user instanceof Gtux_Model_User) 
    {
      throw new LogicException('No existe usuario');
    }
    if (null == $this->_user->getIdUser() && null != $this->_user->getUsername()) 
    {
      $this->_user->findByUsername();
    }
    return $this->_user->getIdUser();
  }

  /**
   * Establece el id del usuario
   * 
   * @param int $id_user
   */
  public function setIdUser($id_user)
  {
    if (Zend_Validate::is($id_user, 'Int'))
    {
      $this->_user = new Gtux_Model_User(array('id_user' => $id_user));
    }
  }

  /**
   * Devuelve el objeto usuario del formulario
   * 
   * @return Gtux_User 
   *
   */
  public function getUser()
  {
    return $this->_user;
  }

  /**
   * Establecen el usuario del formulario
   *
   * @param Gtux_User $user
   * @return Gtux_Model_Abstract
   */
  public function setUser(Gtux_Model_User $user)
  {
    $this->_user = $user;
    return $this;
  }
  
  /**
   *
   * @return Zend_Date
   */
  public function getCreatedAt()
  {
    if (null == $this->_created_at) 
    {
      $this->_created_at = Zend_Date::now(Zend_Registry::get('locale'));
    }
    return $this->_created_at;
  }
  
  /**
   * Establece la fecha de creación
   * 
   * @param Zend_Date $created_at
   */
  public function setCreatedAt(Zend_Date $created_at)
  {
    $this->_created_at = $created_at;
  }
  
}

?>